﻿/*******************************************************************************
 * Copyright @ 2017 YunSpace.Framework 版权所有
 * Author: GaoTang
 * Description: YunSpace 快速开发平台
 * Website：http://www.yunspace.com.cn
*********************************************************************************/
#region USING SYSTEM
using YunSpace.Core;
using YunSpace.Domain.Entity.WeChat;
using YunSpace.Domain.IRepository.WeChat;
using YunSpace.Repository.WeChat;
using System.Collections.Generic;
using System.Linq;
using YunSpace.Data;
using YunSpace.Domain.Entity.SiteMgr;
using YunSpace.Domain.IRepository.SiteMgr;
using YunSpace.Repository.SiteMgr;

#endregion

namespace YunSpace.Application.WeChat
{
    public class UserApp
    {
        private IUserRepository service = new UserRepository();
        private ISiteClientsProjectByContactRepository siteClientsProjectByContactRepository = new SiteClientsProjectByContactRepository();

        public List<WeChatUserEntity> GetList(Pagination pagination, string queryJson)
        {
            var expression = ExtLinq.True<WeChatUserEntity>();
            var queryParam = queryJson.ToJObject();
            // if (!queryParam["keyword"].IsEmpty())
            // {
            //     string keyword = queryParam["keyword"].ToString();
            //     expression = expression.And(t => t.F_Account.Contains(keyword));
            // }
            // if (!queryParam["timeType"].IsEmpty())
            // {
            //     string timeType = queryParam["timeType"].ToString();
            //     DateTime startTime = DateTime.Now.ToString("yyyy-MM-dd").ToDate();
            //     DateTime endTime = DateTime.Now.ToString("yyyy-MM-dd").ToDate().AddDays(1);
            //     switch (timeType)
            //     {
            //         case "1":
            //             break;
            //         case "2":
            //             startTime = DateTime.Now.AddDays(-7);
            //             break;
            //         case "3":
            //             startTime = DateTime.Now.AddMonths(-1);
            //             break;
            //         case "4":
            //             startTime = DateTime.Now.AddMonths(-3);
            //             break;
            //         default:
            //             break;
            //     }
            //     expression = expression.And(t => t.F_Date >= startTime && t.F_Date <= endTime);
            // }
            return service.FindList(expression, pagination);
        }

        public WeChatUserEntity GetForm(string keyValue)
        {
            return service.FindEntity(keyValue);
        }

        public void DeleteForm(string keyValue)
        {
            service.Delete(t => t.F_Id == keyValue);
        }

        public void SubmitForm(WeChatUserEntity userEntity, string keyValue)
        {
            if (!string.IsNullOrEmpty(keyValue))
            {
                userEntity.Modify(keyValue);
                service.Update(userEntity);
            }
            else
            {
                userEntity.Create();
                // userEntity.F_Category = 2;
                service.Insert(userEntity);
            }
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="wxUserId"></param>
        /// <returns></returns>
        public WeChatUserEntity LoginByWxTokenId(string wxUserId)
        {
            return service.FindEntity(x => x.F_WxUserId == wxUserId);
        }

        /// <summary>
        /// 更新手机号
        /// </summary>
        /// <param name="projectByContactId"></param>
        /// <param name="phone"></param>
        public void UpdatePhoneByprojectByContactId(string projectByContactId, string phone)
        {
            var contact = siteClientsProjectByContactRepository.FindEntity(x => x.F_Id == projectByContactId);
            if (contact != null)
            {
                contact.F_Phone = phone;
            }
        }

        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="user"></param>
        /// <param name="phone"></param>
        public void Register(WeChatUserEntity user, string phone)
        {
            var contact = new SiteClientsProjectByContactEntity()
            {
                F_Phone = phone,
                F_ClientId = ""
            };
            contact.Create();
            siteClientsProjectByContactRepository.Insert(contact);

            user.F_ProjectByContactId = contact.F_Id;
            user.Create();
            service.Insert(user);
            //using (var repository = new RepositoryBase())
            //{


            //    //repository.Commit();
            //}
        }

        public WeChatUserEntity Login(string wxUserId)
        {
            var user = service.FindEntity(x => x.F_WxUserId == wxUserId);

            if (user != null)
            {
                return user;
            }

            user = new WeChatUserEntity { F_WxUserId = wxUserId };
            user.Create();

            service.Insert(user);

            return service.FindEntity(x => x.F_WxUserId == wxUserId);
        }

        /// <summary>
        /// 更新或新增用户信息
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public WeChatUserEntity UpdateOrAddUser(WeChatUserEntity entity)
        {
            var user = service.FindEntity(x => x.F_WxUserId == entity.F_WxUserId) ?? new WeChatUserEntity();
            user.F_AvatarUrl = entity.F_AvatarUrl;
            user.F_NickName = entity.F_NickName;
            user.F_City = entity.F_City;
            user.F_Country = entity.F_Country;
            user.F_Gender = entity.F_Gender;
            user.F_Province = entity.F_Province;
            user.F_Language = entity.F_Language;
            user.Modify(user.F_Id);

            if (user.F_Id.IsNullOrEmpty())
            {
                user.F_WxUserId = entity.F_WxUserId;
                user.Create();

                service.Insert(user);
            }
            else
            {
                service.Update(user);
            }


            return service.FindEntity(x => x.F_WxUserId == entity.F_WxUserId);
        }

        public List<WeChatUserEntity> GetAllWeChatUsers()
        {
            return service.IQueryable().ToList();
        }

        /// <summary>
        /// 根据微信用户 id 更新用户信息
        /// </summary>
        /// <param name="wxUserId"></param>
        /// <param name="phone"></param>
        public void UpdateUserPhoneByWxUserId(string wxUserId, string phone)
        {
            var user = service.FindEntity(x => x.F_WxUserId == wxUserId);

            if (user == null) return;

            user.F_Phone = phone;
            user.Modify(user.F_Id);

            service.Update(user);
        }

        public WeChatUserEntity GetUserByWxUserId(string wxUserId)
        {
            return service.FindEntity(x => x.F_WxUserId == wxUserId);
        }

    }
}

